package com.qinsong.myapplication.model;

import android.content.Context;

import com.qinsong.myapplication.EventListener;
import com.qinsong.myapplication.model.bean.UserInfo;
import com.qinsong.myapplication.model.dao.UserAccountDao;
import com.qinsong.myapplication.model.db.DBManager;

import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 定义一个全局类（控制线程）
 */
public class Model {
    private Context mContext;
    private ExecutorService executorService  =Executors.newCachedThreadPool();
    //创建一个对象
    private static Model model = new Model();
    private UserAccountDao userAccountDao;
    private DBManager dbManager;
    //私有化构造方法
    private Model(){

    }

    /**
     * 获取单例对象
     * @return
     */
    public static Model getInstance(){
        return model;
    }

    /**
     * 初始化方法
     * @param context
     */
    public void  init(Context context){
        mContext = context;
        //创建用户数据库操作类
        userAccountDao=new UserAccountDao(mContext);
        //初始化的时候开启全局监听
        EventListener eventListener = new EventListener(mContext);
    }

    /**
     * 获取全局线程池对象
     * @return
     */
    public ExecutorService getGlobaThreadPool(){
        return executorService;
    }

    /**
     * 用户登录成功后处理方法
     * @param account
     */
    public void loginSuccess(UserInfo account) {
        //
        if (account==null){
            return;
        }
        if (dbManager !=null){
            dbManager.close();
        }
        dbManager = new DBManager(mContext, account.getName());
    }

    /**
     * 获取DBmanager操作对象
     * @return
     */
    public DBManager getDbManager(){
        return dbManager;
    }

    /**
     * 获取用户账号数据库的操作类对象
     * @return
     */
    public UserAccountDao getUserAccountDao(){
        return userAccountDao;
    }
}
